package com.financing.platform.task;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.financing.platform.dto.FpBiddingReceiptDTO;
import com.financing.platform.service.IFpBiddingReceiptService;
import com.financing.platform.service.IYouDanAmountService;
import com.financing.platform.util.DateUtil;

/**
 * 
 *
 */
@Component("youdanJob")
public class youdanJob {
	
	private Logger log = Logger.getLogger(this.getClass());
	
	@Autowired
	IFpBiddingReceiptService fpBiddingReceiptService;
	
	@Autowired
	IYouDanAmountService youdanAmountService;
	
	@Scheduled(cron = "0 0/30 * * * ?") 
	public void youdanJob(){
		FpBiddingReceiptDTO fpBiddingReceiptDTO = new FpBiddingReceiptDTO();
		try {
			log.info("youdanJob begin :"+DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
			
			List<Map<String, Object>> queryUnFreezingBailList = fpBiddingReceiptService.queryUnFreezingBailList(fpBiddingReceiptDTO);
			
			if(queryUnFreezingBailList!=null&&queryUnFreezingBailList.size()>0){
				
				//TODO 解冻
				for (int i = 0; i < queryUnFreezingBailList.size(); i++) {
					Map<String, Object> unFreezingBail = queryUnFreezingBailList.get(i);
					try {
						String id=""+unFreezingBail.get("id");
						youdanAmountService.updateBaoZhengJin2Unlock(id, "批处理自动解锁");
					} catch (NumberFormatException e) {
						log.error("unFreezingBail error:"+unFreezingBail.get("id"),e);
					}
				}
				
			}else{
				log.info(DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss")+"---无需要解冻的保证金");
			}
			
			log.info("youdanJob end :"+DateUtil.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
		} catch (SQLException e) {
			log.error("fpBiddingReceiptDTO youdanJob",e);
		}
		
	}
}
